/**
 *
 */
package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.ServiceSetting;
import com.ybkj.daijia.serverUtils.ServiceUtil;
import com.ybkj.daijia.serverUtils.UseAllDataUtil;
import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.DriverRefuseRecordMapper;
import com.ybkj.daijia.server.model.DriverRefuseRecord;
import com.ybkj.daijia.server.service.DriverRefuseRecordService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Shine
 *
 */
@Service
public class DefaultDriverRefuseRecordService implements DriverRefuseRecordService {

    @Autowired
    private DriverRefuseRecordMapper driverRefuseRecordMapper;

    @Transactional(rollbackFor = Exception.class)
    public DriverRefuseRecord save(DriverRefuseRecord driverRefuseRecord) {

        driverRefuseRecordMapper.insertRecord(driverRefuseRecord);

        return driverRefuseRecord;

    }

    @Override
    public DriverRefuseRecord findOne(Long id) {

        return driverRefuseRecordMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {

        driverRefuseRecordMapper.deleteByPrimaryKey(id);

    }

    @Override
    public Page<DriverRefuseRecord> listData(String orderNumber, String employPhone,
        String[] orderType, Date start, Date end) {

        Subject currentUser = SecurityUtils.getSubject();
        Principal principal = (Principal) currentUser.getPrincipal();

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        String likeOrderNumber = null;
        String likeEmployPhone = null;
        if (StringUtils.isNotBlank(orderNumber)) {
            likeOrderNumber = "%" + orderNumber + "%";
        }
        if (StringUtils.isNotBlank(employPhone)) {
            likeEmployPhone = "%" + employPhone + "%";
        }

        if (null == orderType || orderType.length == 0) {
            ServiceSetting serviceSetting = ServiceUtil.checkOpen();
            String orderTypeStr = "";
            if (serviceSetting.isDaijia()) {
                orderTypeStr += DriverRefuseRecord.OrderType.daijia.toString() + ",";
            }
            if (serviceSetting.isZhuanche()) {
                orderTypeStr += DriverRefuseRecord.OrderType.zhuanche.toString() + ",";
            }
            if (serviceSetting.isPaotui()) {
                orderTypeStr += DriverRefuseRecord.OrderType.paotui.toString() + ",";
            }
            if (serviceSetting.isFreight()) {
                orderTypeStr += DriverRefuseRecord.OrderType.freight.toString() + ",";
            }
            if (serviceSetting.isZhuanxian()) {
                orderTypeStr += DriverRefuseRecord.OrderType.zhuanxian.toString() + ",";
            }
            orderTypeStr = orderTypeStr.substring(0, orderTypeStr.length() - 1);
            orderType = orderTypeStr.split(",");
        }

        List<DriverRefuseRecord> content = new ArrayList<DriverRefuseRecord>();
        long total = 0;

        if (principal.isCompanyType()) {
            content = driverRefuseRecordMapper
                .selectList(null, likeOrderNumber, likeEmployPhone, orderType, start, end,
                    Pager.getOffset(), Pager.getLimit());
            total = driverRefuseRecordMapper
                .countList(null, likeOrderNumber, likeEmployPhone, orderType, start, end);
        } else {
            content = driverRefuseRecordMapper
                .selectList(principal.getCompanyId(), likeOrderNumber, likeEmployPhone, orderType,
                    start, end, Pager.getOffset(), Pager.getLimit());
            total = driverRefuseRecordMapper
                .countList(principal.getCompanyId(), likeOrderNumber, likeEmployPhone, orderType,
                    start, end);
        }
        for (DriverRefuseRecord driverRefuseRecord : content) {
            driverRefuseRecord.setEmployPhone(
                UseAllDataUtil.useAllDataDriver(driverRefuseRecord.getEmployPhone()));

        }

        return new PageImpl<DriverRefuseRecord>(content, pageable, total);
    }

    @Override
    public List<DriverRefuseRecord> selectList(String orderNumber,
        String employPhone, String[] orderType, Date start, Date end
    ) {

        Subject currentUser = SecurityUtils.getSubject();
        Principal principal = (Principal) currentUser.getPrincipal();

        String likeOrderNumber = null;
        String likeEmployPhone = null;
        if (StringUtils.isNotBlank(orderNumber)) {
            likeOrderNumber = "%" + orderNumber + "%";
        }
        if (StringUtils.isNotBlank(employPhone)) {
            likeEmployPhone = "%" + employPhone + "%";
        }

        List<DriverRefuseRecord> content = new ArrayList<DriverRefuseRecord>();

        if (principal.isCompanyType()) {
            content = driverRefuseRecordMapper
                .selectList(null, likeOrderNumber, likeEmployPhone, orderType, start, end, null,
                    null);
        } else {
            content = driverRefuseRecordMapper
                .selectList(principal.getCompanyId(), likeOrderNumber, likeEmployPhone, orderType,
                    start, end, null, null);
        }

        return content;
    }

    @Override
    public Long countByOrderIdEmployIdAndOrderType(Long orderId, Long employId, String orderType) {
        return driverRefuseRecordMapper
            .countByOrderIdEmployIdAndOrderType(orderId, employId, orderType);
    }


}
